/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.contextmenu;

import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

import edu.pku.sei.modeler.locator.LocatorMgrsConfigWin;
import edu.pku.sei.modeler.main.Activator;
import edu.pku.sei.modeler.model.core.CoreMetaModel;
import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaReference;
import edu.pku.sei.modeler.model.structure.MetaNode;

/**
 * @author zl
 *
 */
public class ConfigLocationMgrAction extends Action {
	public static final String ID = 
		"edu.pku.modeler.ui.editor.contextmenu.ConfigLocationMgrAction";
	private MetaNode host;
	
	public ConfigLocationMgrAction() {
		super("Configure Location Managers");
		this.setId(ID);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
	 */
	protected boolean calculateEnabled() {
		if(host == null)return false;
		if(!(host.getModelElement() instanceof MetaClass))return false;
		return ((MetaClass)host.getModelElement()).getReferences().size() > 0;
	}

	/* (non-Javadoc)
     * Method declared on IAction.
     */
    public boolean isEnabled() {
        return calculateEnabled();
    }
	public void run() {
		Shell shell = Activator.getDefault().getWorkbench()
			.getActiveWorkbenchWindow().getActivePage().getActivePart()
			.getSite().getShell();
		MetaClass cls = (MetaClass)host.getModelElement();
		
		CoreMetaModel cmm = (CoreMetaModel)cls.getModel();
		List nlist = cmm.getLocationManagers(cls.getElementID());
		
		
		LocatorMgrsConfigWin design = new LocatorMgrsConfigWin(shell, SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL, nlist);
		List refs = cls.getReferences();
		for(int i=0; i<refs.size(); i++) {
			MetaReference ref = (MetaReference)refs.get(i);
			design.addNameMap(ref.getName(), ref.getType().getFullName());
		}
		design.setModel(cls.getModel());
		design.open();
	}
	public MetaNode getHost() {
		return host;
	}

	public void setHost(MetaNode host) {
		this.host = host;
	}

}
